import java.util.List;
import java.util.concurrent.TimeUnit;

import models.Cart;
import models.User;
import play.Application;
import play.GlobalSettings;
import play.libs.Akka;
import play.libs.Yaml;
import scala.concurrent.duration.Duration;

import com.avaje.ebean.Ebean;

import controllers.Authentication;


public class Global extends GlobalSettings {
	@Override
	public void onStart(Application app) {

		if(User.find.findRowCount() == 0)
			Ebean.save((List) Yaml.load("initial-data.yml"));
		
		if(User.find.where().eq("username", "Bob") == null)
		{
			User user = User.create("Bob", "secret", "bob@gmail.com");
			user.role = 1;
			user.save();
		}
		
		Akka.system().scheduler().schedule(Duration.create(0, TimeUnit.MILLISECONDS),
				Duration.create(2, TimeUnit.MINUTES), new Runnable() {
					@Override
					public void run() {
						List<Cart> carts = Cart.find.where().eq("isTemp", "true").findList();
						for(Cart cart : carts)
							cart.delete();
						Authentication.cleanCartId();
					}
				}, Akka.system().dispatcher());
	}
}
